{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f119152d-9d99-4a23-812c-deceae198ce4",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-31T07:06:57.590454Z",
     "iopub.status.busy": "2022-03-31T07:06:57.590142Z",
     "iopub.status.idle": "2022-03-31T07:06:57.682484Z",
     "shell.execute_reply": "2022-03-31T07:06:57.681913Z",
     "shell.execute_reply.started": "2022-03-31T07:06:57.590382Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bdcd8a36-64c1-4c8c-b100-e09544b7524a",
   "metadata": {},
   "source": [
    "## 小结\n",
    "1. NAN: Not A Number\n",
    "    1. 不是一个数字，但是属于浮点型\n",
    "    2. 与任意值进行运算的结果都是NAN\n",
    "    3. NAN != NAN\n",
    "    4. 通过np.isnan来判断某个值是不是nan\n",
    "2. INF：Infinity\n",
    "    * 无穷大，在除数为0的时候会出现\n",
    "3. 处理值的方式\n",
    "    1. 删除值/删除行\n",
    "    2. 替换值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e13ac87d-86c3-43c8-bd9b-ae0e450b30cb",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-31T07:06:58.444895Z",
     "iopub.status.busy": "2022-03-31T07:06:58.444654Z",
     "iopub.status.idle": "2022-03-31T07:06:58.450759Z",
     "shell.execute_reply": "2022-03-31T07:06:58.450182Z",
     "shell.execute_reply.started": "2022-03-31T07:06:58.444868Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3 3 3 0 7]\n",
      " [8 0 2 4 9]\n",
      " [8 6 8 5 9]]\n"
     ]
    }
   ],
   "source": [
    "# 构造3行5列的数组，元素为0到9之间的随机整数\n",
    "data = np.random.randint(0, 10, size=(3, 5))\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "459f74ec-c33e-43e6-b532-54b1f642194d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-31T07:06:59.831496Z",
     "iopub.status.busy": "2022-03-31T07:06:59.831218Z",
     "iopub.status.idle": "2022-03-31T07:07:00.199039Z",
     "shell.execute_reply": "2022-03-31T07:07:00.198131Z",
     "shell.execute_reply.started": "2022-03-31T07:06:59.831467Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "cannot convert float NaN to integer",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Input \u001b[0;32mIn [3]\u001b[0m, in \u001b[0;36m<cell line: 2>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;66;03m# 把a中第1行第1列的元素换成NAN\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m data[\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m1\u001b[39m] \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39mnan\n",
      "\u001b[0;31mValueError\u001b[0m: cannot convert float NaN to integer"
     ]
    }
   ],
   "source": [
    "# 把a中第1行第1列的元素换成NAN\n",
    "data[1, 1] = np.nan"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "cf5ccf94-8128-4874-9fc5-523ef99815fe",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-31T07:07:08.386608Z",
     "iopub.status.busy": "2022-03-31T07:07:08.386219Z",
     "iopub.status.idle": "2022-03-31T07:07:08.391363Z",
     "shell.execute_reply": "2022-03-31T07:07:08.390570Z",
     "shell.execute_reply.started": "2022-03-31T07:07:08.386563Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 3.  3.  3.  0.  7.]\n",
      " [ 8. nan  2.  4.  9.]\n",
      " [ 8.  6.  8.  5.  9.]]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/59/_lbpck214pqcv3v468n7dv6w0000gn/T/ipykernel_2710/2068034831.py:1: DeprecationWarning: `np.float` is a deprecated alias for the builtin `float`. To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.\n",
      "Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
      "  data = data.astype(np.float)\n"
     ]
    }
   ],
   "source": [
    "# 将data的数据类型转换为浮点型\n",
    "data = data.astype(np.float)\n",
    "data[1, 1] = np.nan\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7f759ef1-6a20-41ed-b86b-7777e558c306",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-31T07:07:12.895459Z",
     "iopub.status.busy": "2022-03-31T07:07:12.895214Z",
     "iopub.status.idle": "2022-03-31T07:07:12.899677Z",
     "shell.execute_reply": "2022-03-31T07:07:12.899035Z",
     "shell.execute_reply.started": "2022-03-31T07:07:12.895432Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[inf inf inf nan inf]\n",
      " [inf nan inf inf inf]\n",
      " [inf inf inf inf inf]]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/59/_lbpck214pqcv3v468n7dv6w0000gn/T/ipykernel_2710/335631975.py:1: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  print(data/0)\n",
      "/var/folders/59/_lbpck214pqcv3v468n7dv6w0000gn/T/ipykernel_2710/335631975.py:1: RuntimeWarning: invalid value encountered in true_divide\n",
      "  print(data/0)\n"
     ]
    }
   ],
   "source": [
    "print(data/0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "718c6e86-bc2a-4527-8cd7-097fa4c23748",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-31T07:07:15.848516Z",
     "iopub.status.busy": "2022-03-31T07:07:15.848262Z",
     "iopub.status.idle": "2022-03-31T07:07:15.858441Z",
     "shell.execute_reply": "2022-03-31T07:07:15.857784Z",
     "shell.execute_reply.started": "2022-03-31T07:07:15.848490Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# nan值不能比\n",
    "np.nan == np.nan"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "98d56cde-3ea9-4639-b3bf-e0d711b35442",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-31T07:07:17.364539Z",
     "iopub.status.busy": "2022-03-31T07:07:17.364297Z",
     "iopub.status.idle": "2022-03-31T07:07:17.368989Z",
     "shell.execute_reply": "2022-03-31T07:07:17.368414Z",
     "shell.execute_reply.started": "2022-03-31T07:07:17.364512Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 3.,  3.,  3.,  0.,  7.],\n",
       "       [ 8., nan,  2.,  4.,  9.],\n",
       "       [ 8.,  6.,  8.,  5.,  9.]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b31f824c-a070-48d6-ad02-a526c2218029",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-31T07:15:08.800852Z",
     "iopub.status.busy": "2022-03-31T07:15:08.800446Z",
     "iopub.status.idle": "2022-03-31T07:15:08.806004Z",
     "shell.execute_reply": "2022-03-31T07:15:08.805367Z",
     "shell.execute_reply.started": "2022-03-31T07:15:08.800824Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[False, False, False, False, False],\n",
       "       [False,  True, False, False, False],\n",
       "       [False, False, False, False, False]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 识别data中的nan元素\n",
    "np.isnan(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "43ca4534-8bda-4d5c-8e67-8991af81bd9a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-31T07:26:23.639213Z",
     "iopub.status.busy": "2022-03-31T07:26:23.638964Z",
     "iopub.status.idle": "2022-03-31T07:26:23.645278Z",
     "shell.execute_reply": "2022-03-31T07:26:23.644650Z",
     "shell.execute_reply.started": "2022-03-31T07:26:23.639185Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3., 3., 3., 0., 7., 8., 2., 4., 9., 8., 6., 8., 5., 9.])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 删除data中nan元素\n",
    "data[~np.isnan(data)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "6a181a16-7608-48d7-b7dc-2ffd4272c2e5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-31T09:00:08.957223Z",
     "iopub.status.busy": "2022-03-31T09:00:08.956976Z",
     "iopub.status.idle": "2022-03-31T09:00:08.964054Z",
     "shell.execute_reply": "2022-03-31T09:00:08.963020Z",
     "shell.execute_reply.started": "2022-03-31T09:00:08.957196Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([1]), array([1]))"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取data中nan元素的位置信息\n",
    "np.where(np.isnan(data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "f96257a8-ccf4-4d08-b876-5eff409bbd9f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-31T09:11:24.819736Z",
     "iopub.status.busy": "2022-03-31T09:11:24.819490Z",
     "iopub.status.idle": "2022-03-31T09:11:24.825633Z",
     "shell.execute_reply": "2022-03-31T09:11:24.824389Z",
     "shell.execute_reply.started": "2022-03-31T09:11:24.819711Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取data中nan元素的行下标信息\n",
    "np.where(np.isnan(data))[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "3223b1ce-234e-4e35-8a18-b17eae24749d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-31T09:23:05.935950Z",
     "iopub.status.busy": "2022-03-31T09:23:05.935704Z",
     "iopub.status.idle": "2022-03-31T09:23:05.941784Z",
     "shell.execute_reply": "2022-03-31T09:23:05.941058Z",
     "shell.execute_reply.started": "2022-03-31T09:23:05.935923Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3., 3., 3., 0., 7.],\n",
       "       [8., 6., 8., 5., 9.]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取data中nan元素的行下标信息\n",
    "lines = np.where(np.isnan(data))[0]\n",
    "# 删除data中有nan值的行\n",
    "np.delete(data, lines, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "fe7d416f-756a-482b-a22e-e11e67a0a1f6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-31T16:37:13.689765Z",
     "iopub.status.busy": "2022-03-31T16:37:13.689401Z",
     "iopub.status.idle": "2022-03-31T16:37:14.012204Z",
     "shell.execute_reply": "2022-03-31T16:37:14.011376Z",
     "shell.execute_reply.started": "2022-03-31T16:37:13.689740Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "could not convert string to float: ''",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Input \u001b[0;32mIn [17]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m scores \u001b[38;5;241m=\u001b[39m \u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mloadtxt\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mdata/nan_scores.csv\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdelimiter\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m,\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mskiprows\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m/opt/miniconda3/envs/jpt/lib/python3.10/site-packages/numpy/lib/npyio.py:1163\u001b[0m, in \u001b[0;36mloadtxt\u001b[0;34m(fname, dtype, comments, delimiter, converters, skiprows, usecols, unpack, ndmin, encoding, max_rows, like)\u001b[0m\n\u001b[1;32m   1159\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m   1160\u001b[0m             \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mWrong number of columns at line \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mlineno\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m   1161\u001b[0m     \u001b[38;5;66;03m# Convert each value according to its column, then pack it\u001b[39;00m\n\u001b[1;32m   1162\u001b[0m     \u001b[38;5;66;03m# according to the dtype's nesting, and store it.\u001b[39;00m\n\u001b[0;32m-> 1163\u001b[0m     chunk\u001b[38;5;241m.\u001b[39mappend(packer(\u001b[43mconvert_row\u001b[49m\u001b[43m(\u001b[49m\u001b[43mwords\u001b[49m\u001b[43m)\u001b[49m))\n\u001b[1;32m   1164\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m chunk:  \u001b[38;5;66;03m# The islice is empty, i.e. we're done.\u001b[39;00m\n\u001b[1;32m   1165\u001b[0m     \u001b[38;5;28;01mbreak\u001b[39;00m\n",
      "File \u001b[0;32m/opt/miniconda3/envs/jpt/lib/python3.10/site-packages/numpy/lib/npyio.py:1142\u001b[0m, in \u001b[0;36mloadtxt.<locals>.convert_row\u001b[0;34m(vals, _conv)\u001b[0m\n\u001b[1;32m   1141\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mconvert_row\u001b[39m(vals, _conv\u001b[38;5;241m=\u001b[39mconverters[\u001b[38;5;241m0\u001b[39m]):\n\u001b[0;32m-> 1142\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m [\u001b[38;5;241m*\u001b[39m\u001b[38;5;28mmap\u001b[39m(_conv, vals)]\n",
      "File \u001b[0;32m/opt/miniconda3/envs/jpt/lib/python3.10/site-packages/numpy/lib/npyio.py:725\u001b[0m, in \u001b[0;36m_floatconv\u001b[0;34m(x)\u001b[0m\n\u001b[1;32m    723\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_floatconv\u001b[39m(x):\n\u001b[1;32m    724\u001b[0m     \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 725\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mfloat\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mx\u001b[49m\u001b[43m)\u001b[49m  \u001b[38;5;66;03m# The fastest path.\u001b[39;00m\n\u001b[1;32m    726\u001b[0m     \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m:\n\u001b[1;32m    727\u001b[0m         \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m0x\u001b[39m\u001b[38;5;124m'\u001b[39m \u001b[38;5;129;01min\u001b[39;00m x:  \u001b[38;5;66;03m# Don't accidentally convert \"a\" (\"0xa\") to 10.\u001b[39;00m\n",
      "\u001b[0;31mValueError\u001b[0m: could not convert string to float: ''"
     ]
    }
   ],
   "source": [
    "# 读取nan_scores.csv到scores里\n",
    "scores = np.loadtxt(\"data/nan_scores.csv\", delimiter=\",\", skiprows=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "920772b9-468a-484b-b6ed-966302a8cc6d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-31T18:00:55.285332Z",
     "iopub.status.busy": "2022-03-31T18:00:55.285023Z",
     "iopub.status.idle": "2022-03-31T18:00:55.292249Z",
     "shell.execute_reply": "2022-03-31T18:00:55.291264Z",
     "shell.execute_reply.started": "2022-03-31T18:00:55.285299Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/59/_lbpck214pqcv3v468n7dv6w0000gn/T/ipykernel_2710/1533672581.py:2: DeprecationWarning: `np.str` is a deprecated alias for the builtin `str`. To silence this warning, use `str` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.str_` here.\n",
      "Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
      "  scores = np.loadtxt(\"data/nan_scores.csv\", delimiter=\",\", skiprows=1, dtype=np.str)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([['59', '89'],\n",
       "       ['90', '32'],\n",
       "       ['78', '45.5'],\n",
       "       ['34', ''],\n",
       "       ['', '56'],\n",
       "       ['23', '56']], dtype='<U4')"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读取nan_scores.csv到scores里\n",
    "scores = np.loadtxt(\"data/nan_scores.csv\", delimiter=\",\", skiprows=1, dtype=np.str)\n",
    "# 查看\n",
    "scores"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "f7d5b6b7-5cbe-4f34-b31e-472f2b884a7f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-31T18:00:57.190014Z",
     "iopub.status.busy": "2022-03-31T18:00:57.189764Z",
     "iopub.status.idle": "2022-03-31T18:00:57.194493Z",
     "shell.execute_reply": "2022-03-31T18:00:57.193857Z",
     "shell.execute_reply.started": "2022-03-31T18:00:57.189987Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([['59', '89'],\n",
       "       ['90', '32'],\n",
       "       ['78', '45.5'],\n",
       "       ['34', 'nan'],\n",
       "       ['nan', '56'],\n",
       "       ['23', '56']], dtype='<U4')"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 把空字符串转换成nan\n",
    "scores[scores == \"\"] = np.nan\n",
    "scores"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "09bbd13e-6698-4b88-8662-1546d68cc628",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-31T18:00:59.669796Z",
     "iopub.status.busy": "2022-03-31T18:00:59.669553Z",
     "iopub.status.idle": "2022-03-31T18:00:59.675483Z",
     "shell.execute_reply": "2022-03-31T18:00:59.674680Z",
     "shell.execute_reply.started": "2022-03-31T18:00:59.669770Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/59/_lbpck214pqcv3v468n7dv6w0000gn/T/ipykernel_2710/1771949420.py:2: DeprecationWarning: `np.float` is a deprecated alias for the builtin `float`. To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.\n",
      "Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
      "  scores1 = scores.astype (np.float)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[59. , 89. ],\n",
       "       [90. , 32. ],\n",
       "       [78. , 45.5],\n",
       "       [34. ,  nan],\n",
       "       [ nan, 56. ],\n",
       "       [23. , 56. ]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组dtype都换成浮点型\n",
    "scores1 = scores.astype (np.float)\n",
    "scores1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "e712d31e-402f-45bd-9c8c-72610e28f93d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-31T18:01:01.733625Z",
     "iopub.status.busy": "2022-03-31T18:01:01.733381Z",
     "iopub.status.idle": "2022-03-31T18:01:01.738438Z",
     "shell.execute_reply": "2022-03-31T18:01:01.737804Z",
     "shell.execute_reply.started": "2022-03-31T18:01:01.733600Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[59. , 89. ],\n",
       "       [90. , 32. ],\n",
       "       [78. , 45.5],\n",
       "       [34. ,  0. ],\n",
       "       [ 0. , 56. ],\n",
       "       [23. , 56. ]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 把scores1中的nan替换成0\n",
    "scores1[np.isnan(scores1)] = 0\n",
    "scores1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "2c89ed60-0da5-49f3-b277-2a5cf48a9650",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-31T18:01:07.820589Z",
     "iopub.status.busy": "2022-03-31T18:01:07.820345Z",
     "iopub.status.idle": "2022-03-31T18:01:07.825117Z",
     "shell.execute_reply": "2022-03-31T18:01:07.824523Z",
     "shell.execute_reply.started": "2022-03-31T18:01:07.820562Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([148. , 122. , 123.5,  34. ,  56. ,  79. ])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores1.sum(axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "b44835f2-8693-47d8-a9ce-54caa316622e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-31T18:05:36.558187Z",
     "iopub.status.busy": "2022-03-31T18:05:36.557936Z",
     "iopub.status.idle": "2022-03-31T18:05:36.563427Z",
     "shell.execute_reply": "2022-03-31T18:05:36.562675Z",
     "shell.execute_reply.started": "2022-03-31T18:05:36.558159Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/59/_lbpck214pqcv3v468n7dv6w0000gn/T/ipykernel_2710/1753985862.py:1: DeprecationWarning: `np.float` is a deprecated alias for the builtin `float`. To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.\n",
      "Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
      "  scores2 = scores.astype (np.float)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[59. , 89. ],\n",
       "       [90. , 32. ],\n",
       "       [78. , 45.5],\n",
       "       [34. ,  nan],\n",
       "       [ nan, 56. ],\n",
       "       [23. , 56. ]])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores2 = scores.astype (np.float)\n",
    "scores2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "e9608163-c27e-4ef0-8bde-c12e2956bf9e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-31T18:18:42.124108Z",
     "iopub.status.busy": "2022-03-31T18:18:42.123862Z",
     "iopub.status.idle": "2022-03-31T18:18:42.131553Z",
     "shell.execute_reply": "2022-03-31T18:18:42.130166Z",
     "shell.execute_reply.started": "2022-03-31T18:18:42.124081Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[59. , 89. ],\n",
       "       [90. , 32. ],\n",
       "       [78. , 45.5],\n",
       "       [34. , 55.7],\n",
       "       [56.8, 56. ],\n",
       "       [23. , 56. ]])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 每门课把nan替换成均值\n",
    "for x in range(scores2.shape[1]):\n",
    "    col = scores2[:,x] # 取第x列的所有元素\n",
    "    mean = col[~np.isnan(col)].mean() # 找到当前列的所有非nan值，并求它们的均值\n",
    "    col[np.isnan(col)] = mean # 用求出来的均值替换nan值\n",
    "scores2"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
